<?php


namespace App\Libraries\CustomsBills;


use App\Libraries\CustomsBills\Data\BaseTransfer;
use App\Libraries\CustomsBills\Data\CEB311Message;
use InvalidArgumentException;

class Message extends Contracts\Message
{
    /**
     * 报文对应处理类
     * @var array
     * @author luffyzhao@vip.126.com
     */
    protected $messageTypes = [
        'CEB311Message' => CEB311Message::class
    ];

    /**
     * @var string
     * @author luffyzhao@vip.126.com
     */
    protected $messageType;

    /**
     * 一个文件最大报文个数
     * @var int
     * @author luffyzhao@vip.126.com
     */
    protected $messageCount = 500;

    /**
     * @var array
     * @author luffyzhao@vip.126.com
     */
    protected $messageList = [];

    /**
     * @var BaseTransfer
     * @author luffyzhao@vip.126.com
     */
    protected $baseTransfer;

    /**
     * Message constructor.
     * @param $messageType
     * @author luffyzhao@vip.126.com
     */
    public function __construct($messageType)
    {
        if (in_array($messageType, $this->getMessageTypes())) {
            $this->messageType = $messageType;
        } else {
            throw new InvalidArgumentException('参数错误');
        }
    }

    /**
     * @return array
     * @author luffyzhao@vip.126.com
     */
    public function getMessageList(): array
    {
        return $this->messageList;
    }

    /**
     * @param $messageList
     * @author luffyzhao@vip.126.com
     */
    public function addMessageList($messageList): void
    {
        if ($messageList instanceof $this->messageType) {
            $this->messageList[] = $messageList;
        } else {
            throw new InvalidArgumentException('参数错误');
        }
    }

    /**
     * @return array
     * @author luffyzhao@vip.126.com
     */
    public function getMessageTypes(): array
    {
        return $this->messageTypes;
    }

    /**
     * @return string
     * @author luffyzhao@vip.126.com
     */
    public function getMessageType()
    {
        return $this->messageType;
    }

    /**
     * @return false|int|string
     * @author luffyzhao@vip.126.com
     */
    public function getXmlRootNode()
    {
        return array_search($this->getMessageType(), $this->getMessageTypes());
    }

    /**
     * @return BaseTransfer
     * @author luffyzhao@vip.126.com
     */
    public function getBaseTransfer(): BaseTransfer
    {
        return $this->baseTransfer;
    }

    /**
     * @param BaseTransfer $baseTransfer
     * @author luffyzhao@vip.126.com
     */
    public function setBaseTransfer(BaseTransfer $baseTransfer): void
    {
        $this->baseTransfer = $baseTransfer;
    }

    /**
     * @return string
     * @author luffyzhao@vip.126.com
     */
    public function __toString()
    {
        return <<<MESSAGE
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<ceb:{$this->getXmlRootNode()} guid="{$this->getUUid()}" version="1.0" xmlns:ceb="http://www.chinaport.gov.cn/ceb" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
{$this->getImplode($this->getMessageList())}
{$this->getBaseTransfer()}
</ceb:{$this->getXmlRootNode()}>
MESSAGE;
    }
}